package com.itts.common.util.result;

import lombok.Data;

import java.util.LinkedHashMap;
import java.util.Map;

/**
 * 查询参数
 */
@Data
public class Query extends LinkedHashMap<String, Object> {
    private static final long serialVersionUID = 1L;
    //
    private int offset = 0;
    // 每页条数
    private int pageSize = 10;

    private int pageNum = 1;

    private int total = 0;//..导出处有值

    public Query(Map<String, Object> params) {
        this.putAll(params);
        if (params.get("offset") != null) {
            this.offset = Integer.parseInt(params.get("offset").toString());
        }
        if (params.get("pageSize") != null) {
            this.pageSize = Integer.parseInt(params.get("pageSize").toString());
        }
        if (params.get("pageNum") != null) {
            this.pageNum = Integer.parseInt(params.get("pageNum").toString());
        } else {
            this.put("pageNum", offset / pageSize + 1);
        }
        this.put("offset", offset);
        this.put("pageSize", pageSize);
    }

    public Integer getIntVal(String key) {
        Object val = this.get(key);
        if (val == null) {
            throw new RuntimeException("不存在键：key=" + key);
        }
        if (val instanceof Integer) {
            return (int) val;
        } else {
            try {
                Integer value = Integer.valueOf(String.valueOf(val));
                return value;
            } catch (Exception e) {
                throw new RuntimeException("值类型不支持Integer类型，key=" + key + ",val=" + val);
            }
        }
    }

    public Long getLongVal(String key) {
        Object val = this.get(key);
        if (val == null) {
            throw new RuntimeException("不存在键：key=" + key);
        }
        if (val instanceof Long) {
            return (long) val;
        }
        if (val instanceof Integer) {
            return (long) (int) val;
        } else {
            try {
                Long value = Long.valueOf(String.valueOf(val));
                return value;
            } catch (Exception e) {
                throw new RuntimeException("值类型不支持Long类型，key=" + key + ",val=" + val);
            }
        }
    }

    public Float getFloatVal(String key) {
        Object val = this.get(key);
        if (val == null) {
            throw new RuntimeException("不存在键：key=" + key);
        }
        if (val instanceof Float) {
            return Float.valueOf(String.valueOf(val));
        } else {
            try {
                Float value = Float.valueOf(String.valueOf(val));
                return value;
            } catch (Exception e) {
                throw new RuntimeException("值类型不支持Float类型，key=" + key + ",val=" + val);
            }
        }
    }

    public Double getDoubleVal(String key) {
        Object val = this.get(key);
        if (val == null) {
            throw new RuntimeException("不存在键：key=" + key);
        }
        if (val instanceof Double || val instanceof Float) {
            return Double.valueOf(String.valueOf(val));
        } else {
            try {
                Double value = Double.valueOf(String.valueOf(val));
                return value;
            } catch (Exception e) {
                throw new RuntimeException("值类型不支持Double类型，key=" + key + ",val=" + val);
            }
        }
    }

    public String getStringVal(String key) {
        Object val = this.get(key);
        if (val == null) {
            throw new RuntimeException("不存在键：key=" + key);
        }
        return String.valueOf(val);
    }

}

